package com.lanou.service.impl;

import com.alibaba.fastjson.JSON;
import com.lanou.entity.ContentImg;
import com.lanou.entity.TbContent;
import com.lanou.entity.TbUser;
import com.lanou.service.IndexService;
import com.lanou.utils.HttpClientUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class IndexServiceImpl implements IndexService {

    @Value("${CAT_JSON_URL}")
    private String CAT_JSON_URL;
    @Value("${INDEX_CONTENT_JSON_URL}")
    private String INDEX_CONTENT_JSON_URL;
    @Value("${GET_USER_COOKIE_URL}")
    private String GET_USER_COOKIE_URL;
    @Value("${QUIT_USER_COOKIE_URL}")
    private String QUIT_USER_COOKIE_URL;

    @Value("${INDEX_CONTENT_IMG_CID}")
    private Long INDEX_CONTENT_IMG_CID;
    @Value("${INDEX_CONTENT_IMG_HEIGHT}")
    private Integer INDEX_CONTENT_IMG_HEIGHT;
    @Value("${INDEX_CONTENT_IMG_WIDTH}")
    private Integer INDEX_CONTENT_IMG_WIDTH;
    @Value("${INDEX_CONTENT_IMG_WIDTHB}")
    private Integer INDEX_CONTENT_IMG_WIDTHB;
    @Value("${INDEX_CONTENT_IMG_HEIGHTB}")
    private Integer INDEX_CONTENT_IMG_HEIGHTB;


    @Override
    public Map<String, Object> listCat() {
        String catList = HttpClientUtils.getRaw(CAT_JSON_URL);
        List<Object> list = JSON.parseObject(catList).getJSONArray("data").toJavaList(Object.class);
        List<Map<String, Object>> oneList = getList(list);
        Map<String, Object> map = new HashMap<>();
        map.put("data", oneList);
        return map;
    }

    @Override
    public List<ContentImg> showContentImg() {
        List<TbContent> contentList = HttpClientUtils.getList(INDEX_CONTENT_JSON_URL + INDEX_CONTENT_IMG_CID, TbContent.class);
        List<ContentImg> contentImgList = new ArrayList<>();
        for (TbContent tbContent : contentList) {
            ContentImg contentImg = new ContentImg();
            contentImg.setAlt(tbContent.getTitle());
            contentImg.setSrcB(tbContent.getPic());
            contentImg.setHeight(INDEX_CONTENT_IMG_HEIGHT);
            contentImg.setWidth(INDEX_CONTENT_IMG_WIDTH);
            contentImg.setSrc(tbContent.getPic2());
            contentImg.setWidthB(INDEX_CONTENT_IMG_WIDTHB);
            contentImg.setHref(tbContent.getUrl());
            contentImg.setHeightB(INDEX_CONTENT_IMG_HEIGHTB);
            contentImgList.add(contentImg);
        }
        System.out.println(contentImgList);
        return contentImgList;
    }

    @Override
    public TbUser getUser(String cookie) {
        TbUser user = HttpClientUtils.getObject(GET_USER_COOKIE_URL + cookie, TbUser.class);
        return user;
    }

    @Override
    public TbUser quitUser(String cookie) {
        TbUser user = HttpClientUtils.getObject(QUIT_USER_COOKIE_URL + cookie, TbUser.class);
        return user;
    }


    private List<Map<String, Object>> getList(List oneList) {
        List list = new ArrayList<>();
        for (Object o : oneList) {
            Map<String,Object> map = (Map<String, Object>) o;
            Map<String, Object> catMap = new HashMap<>();
            if (map.containsKey("childList")) {
                int parentId = (int) map.get("parentId");
                if (parentId == 0) {
                    catMap.put("n", "<a href='/products/" + map.get("id") + ".html'>" + map.get("name") + "</a>");
                } else {
                    catMap.put("n", map.get("name"));
                }
                List<Map<String, Object>> childList1 = (List<Map<String, Object>>) map.get("childList");
                List<Map<String, Object>> childList = getList(childList1);
                catMap.put("u", "/products/" + map.get("id") + ".html");
                catMap.put("i", childList);
                list.add(catMap);
            } else {
                list.add("/products/" + map.get("id") + ".html|" + map.get("name"));
            }

        }
        return list;
    }

}
